package input;

/**
 * The Class Keys is responsible for saving all pressed keys 
 * and provides this information for other classes.
 */
public class Keys {
	
	/**
	 * Instantiates a new Keys object.
	 */
	public Keys() {
		oldKeyStates = 0;
		pressedKeys = releasedKeys = 0;
	}
	
	/**
	 * Stores the new key states.
	 *
	 * @param keyStates the new key states
	 */
	public void Update(int keyStates) {
		pressedKeys = keyStates;

		releasedKeys = (oldKeyStates ^ pressedKeys) & (pressedKeys ^ -1);
		
		oldKeyStates = keyStates;
	}
	
	/**
	 * Checks if a specific key is pressed.
	 *
	 * @param key the key number
	 * @return true, if is pressed
	 */
	public boolean IsPressed(int key) {
		return (pressedKeys & key) != 0;
	}
	
	/**
	 * Checks if a specific key is released.
	 *
	 * @param key the key number
	 * @return true, if is released
	 */
	public boolean IsReleased(int key) {
		return (releasedKeys & key) != 0;
	}
	
	/** The old key states. */
	private int oldKeyStates;
	
	/** The pressed keys. */
	private int pressedKeys;
	
	/** The released keys. */
	private int releasedKeys;
}
